#region Using Statements
using System;
using System.Xml;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SkyBurner.Screens;
#endregion

namespace SkyBurner
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SkyBurnerGame : BaseGame
    {
        private XMLPropertyBag m_configPropertyBag;

        public SkyBurnerGame()
            : base()
        {
            m_texturePath = "Data\\Textures";

            XmlDocument xmlConfig = new XmlDocument();
            try
            {
                xmlConfig.Load("Data\\Config\\Config.xml");
                m_configPropertyBag = new XMLPropertyBag(xmlConfig.DocumentElement);

                m_graphicsDeviceManager.PreferredBackBufferWidth = m_configPropertyBag.GetValue<int>("ScreenWidth", XMLPropertyBagValueType.vtInteger);
                m_graphicsDeviceManager.PreferredBackBufferHeight = m_configPropertyBag.GetValue<int>("ScreenHeight", XMLPropertyBagValueType.vtInteger);
                m_graphicsDeviceManager.IsFullScreen = (m_configPropertyBag.GetValue<int>("Fullscreen", XMLPropertyBagValueType.vtInteger) != 0);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            Console.Print(String.Format("SkyBurner Game ver {0} by Riccardo Ceccato", m_Version));
            Console.Print("   http://moriarty72.spaces.live.com/");
            Console.Print("");

            // load configuration...
            int nEnableAudio = 0;
            try
            {
                nEnableAudio = m_configPropertyBag.GetValue<int>("AudioEnabled", XMLPropertyBagValueType.vtInteger);
                Console.Enabled = (m_configPropertyBag.GetValue<int>("ConsoleEnabled", XMLPropertyBagValueType.vtInteger) != 0);
                UserInputState.KeyboardSensibility = m_configPropertyBag.GetValue<float>("KeyboardSensibility", XMLPropertyBagValueType.vtFloat);
            }
            catch (Exception e)
            {
                Console.Print("SkyBurnerGame.Initialize exception: " + e.Message);
            }

            // setup bitmap font
            m_bitmapFont = new BitmapFont(this, "Data\\Textures\\HUD\\bw_Letters", "Data\\Textures\\HUD\\bw_Numbers", 26);            
            
            // initialize managers...
            m_soundManager.Enabled = (nEnableAudio != 0);

            m_soundManager.Initialize("Data\\Sounds", "SkyBurnerSounds.xgs");
            m_shadersManager.Load("Data\\Shaders\\Shaders.xml");
            
            // load hi score table...
            try
            {                
                HiscoreTable.Instance().Load("Data\\Config\\Hiscores.xml");
            }
            catch (Exception e)
            {
                Console.Print("SkyBurnerGame.Initialize Hiscore exception: " + e.Message);
            }

            // add screens...
            m_screenManager.AddScreen(new SkyBurnerScreenMain(this, m_screenManager, (int)SkyBurnerScreenID.sbs_Main, false));
            m_screenManager.AddScreen(new SkyBurnerScreenLevelChooser(this, m_screenManager, (int)SkyBurnerScreenID.sbs_LevelChooser, false));
            m_screenManager.AddScreen(new SkyBurnerScreenInGame(this, m_screenManager, (int)SkyBurnerScreenID.sbs_Game, false));
            m_screenManager.AddScreen(new SkyBurnerScreenCredits(this, m_screenManager, (int)SkyBurnerScreenID.sbs_Credits, false));
            m_screenManager.AddScreen(new SkyBurnerScreenHiScores(this, m_screenManager, (int)SkyBurnerScreenID.sbs_Highscores, false));
            m_screenManager.AddScreen(new SkyBurnerScreenConfig(this, m_screenManager, (int)SkyBurnerScreenID.sbs_Config, false));
            m_screenManager.ActivateScreen(SkyBurnerScreenID.sbs_Main);

            // let's play sound...
            m_soundManager.PlayBackgroundMusic("MainTheme");            
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
            }

            // TODO: Load any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                m_contentManager.Unload();
            }

            // TODO: Unload any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            //if (m_userInputState.IsUserAction(UserAction.Back))
            //    this.Exit();

            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}
